package com.casic.modules.jy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.casic.common.utils.R;
import com.casic.common.base.entity.PageEntity;
import com.casic.modules.jy.entity.JySjXsEntity;
import com.casic.modules.jy.entity.JyXsKcEntity;
import com.casic.modules.jy.service.JySjXsService;
import com.casic.modules.jy.service.JyXsKcService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.casic.modules.jy.entity.JySjInfoEntity;
import com.casic.modules.jy.service.JySjInfoService;


/**
 * 试卷信息表
 * 
 * @author mfc
 * @email 123456789@qq.com
 * @date 2018-09-25 14:55:22
 */
@RestController
@RequestMapping("jysjinfo")
public class JySjInfoController {
	@Autowired
	private JySjInfoService jySjInfoService;
	@Autowired
	private JySjXsService jySjXsService;
	@Autowired
	private JyXsKcService jyXsKcService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")

	public R list(@RequestParam Map<String, Object> params){
        PageEntity<JySjInfoEntity> page=jySjInfoService.findPage(params);
		return R.ok().put("page", page);
	}

	@RequestMapping("/findList")
	public R findList(@RequestParam Map<String, Object> params){
		List<JySjInfoEntity> list = jySjInfoService.findList(params);
		return R.ok().put("page", list);
	}

	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")

	public R info(@PathVariable("id") String id){
		JySjInfoEntity jySjInfo = jySjInfoService.findById(id);
		return R.ok().put("jySjInfo", jySjInfo);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")

	public R save(@RequestBody JySjInfoEntity jySjInfo){
		JySjInfoEntity bean = jySjInfoService.save(jySjInfo);
		return R.ok().put("id",bean.getId());
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")

	public R update(@RequestBody JySjInfoEntity jySjInfo){
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("kcId",jySjInfo.getKcId());
		List<JyXsKcEntity> xskcList = jyXsKcService.findList(param);
		if(xskcList !=null && xskcList.size()>0){
			for(JyXsKcEntity entity : xskcList){
				JySjXsEntity sj = new JySjXsEntity() ;
				sj.setXsId(entity.getXh());
				sj.setSjId(jySjInfo.getId());
				sj.setState("0");
				jySjXsService.save(sj);
			}
		}
		jySjInfoService.edit(jySjInfo);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")

	public R delete(@RequestBody String[] ids){
		jySjInfoService.removeByIds(ids);
		return R.ok();
	}

	/**
	 * 参考试卷保存
	 */
	@RequestMapping("/copySave")

	public R copySave(@RequestBody JySjInfoEntity jySjInfo){
		JySjInfoEntity bean = jySjInfoService.copySave(jySjInfo);
		return R.ok().put("id",bean.getId());
	}
}
